<?php
namespace Admin\Controller;
use Common\Controller\CommonController;
class AuthController extends CommonController{
    public function index(){
        $this->display();
    }
    /**
     * 添加角色
     */
    public function addRole(){
        // var_dump(I());
        $info['name'] = I('name');
        $info['remark'] = I('remark');
        $status = I('status');
        if ($status == 1) {
            $info['status'] = 1;
        }else{
             $info['status'] = 0;
        }
        // var_dump($status);
        if ($_POST) {
        $re = M ('role')->add ($info);
 
        // if ($_POST) {
        //     if (M ('role')->add ($info)) {
        //         // $this->behaviorLog("成功添加角色");
        //         $data['status']=100;
        //         $data['info']="添加角色信息成功！";
        //         $this->ajaxReturn($data);
        //     } else {
        //         // $this->behaviorLog("添加角色失败");
        //         $data['status']=102;
        //         $data['info']="添加用户失败";
        //         $this->ajaxReturn($data);
        //     }

        if ($re) {
        $this->success("添加成功",U('Auth/roleList'));
       }else{
         $this->error("失败");
       }       



        } else {
            $this->display ();
        }
    }



    /**
     * 角色列表
     */
    public function roleList() {
         $pageno = I('p',1);
         $pageCount = 15;
         $list = M('role')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();

              
         foreach($list as  $key => $item ){
            // $list[$key]['a_typename']  = M('Archive_type')->where('at_typeid="'.$item['a_type'].'"')->getField('at_type_name');
         }
        
         $count = M('role')->where($where)->count();// 查询满足要求的总记录数
         // $this->assign('count',$count);// 赋值分页输出    
         $Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
         $show = $Page->show();// 分页显示输出
         // $this->assign('typelist', $typeList);
         // $this->assign('shArray',$shArray);// 搜索参数输出
         $this->assign('pageno',$show);// 赋值分页输出    
         $this->assign('rolelist', $list);

         // var_dump($list);
         $this->display();
         }
 




 // public function gettree(){
 //        $list = M('node')->select();
 //        $data = array();
 //        foreach($list as $k => $v){
 //            if($v['pid'] == 0){
 //                $data[]=$v[];
 //                foreach($list as $k1 => $v1){
 //                    if($v['id'] == $v1['pid']){
 //                        $data[] = '---'.$v1[];
 //                        foreach ($list as $key => $value) {
 //                            if($v1['id'] == $value['pid']){
 //                                $data[]='------'.$value[];
 //                            }
 //                        }
 //                    }
 //                }
 //            }
 //        }
 //    }



    /**
     * 添加权限
     */
    // ,U('Auth/nodeList')
    public function addNode() {
        $node = M ( 'node' );
        $info['name'] = I('name');
        $info['title'] = I('title');     
        $info['status'] = I('status');
        $info['sort'] = I('sort');
        $info['pid'] = I('pid');
        if ($info['title'] != null) {
            $pid = $node->where ( "id={$info['pid']}" )->select ();
             $info['cname'] = $pid[0]['cname'];   
                

            $re = $node->add($info);
              if ($re) {
                $this->success("添加成功");
             }else{
                 $this->error("失败");
              }   
            // if($node->add($_POST)){
            //     $this->behaviorLog("成功添加权限信息");
            //     $data['status']=100;
            //     $data['info']="添加权限信息成功！";
            //     $this->ajaxReturn($data);
            //    //$this->success("添加成功");
            // }else{
            //     $this->behaviorLog("添加权限信息失败");
            //     $this->error("添加失败");
            // }
        }else{
            $list = $node->where ( "level!=3" )->order ( "sort asc" )->select ();
            $info = $node->where ( array('pid'=>0) )->select();
            // $infol = $node->where ( "pid={$info['id']}" )->select();
            $this->assign ( "list", $list );
                        $data = array();
                        foreach($info as $k => $v){
                            if($v['pid'] == 0){
                                $data[]=$v;
                                foreach($list as $k1 => $v1){
                                    if($v['id'] == $v1['pid']){
                                        $v1['title'] = '---'.$v1['title'];
                                        $data[] = $v1;
                                        foreach ($list as $key => $value) {
                                            if($v1['id'] == $value['pid']){
                                                $value['title'] = '------'.$value['title'];
                                                $data[]=$value;
                                            }
                                        }
                                    }
                                }
                            }
                        }

            $this->assign ( "info", $info);
            $this->assign ( "data", $data);
            $this->display ();
        }

    }

    /**
    * 删除权限 lhx
    */
    public function delNode(){
        $mod=M("role");
        $res=$mod->where("id={$_GET['id']}")->delete();
        if($res){
             $data['status']=100;
             $data['info']="成功删除角色！";
             $this->ajaxReturn($data); 
             $this->behaviorLog("成功删除角色");
             $this->redirect('Auth/roleList');

        }else{
             $this->behaviorLog("删除角色失败");
             $this->redirect('Auth/roleList');
        }

    }


    /**
     * 权限管理
     */
    public function nodeList() {
         $pageno = I('p',1);
         $pageCount = 10;
         $list = M('Node')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
         //$typeList = M('Archive_type')->select();
              
         foreach($list as  $key => $item ){
            // $list[$key]['a_typename']  = M('Archive_type')->where('at_typeid="'.$item['a_type'].'"')->getField('at_type_name');
         }
        
         $count = M('Node')->where($where)->count();// 查询满足要求的总记录数
         // $this->assign('count',$count);// 赋值分页输出    
         $Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
         $show = $Page->show();// 分页显示输出
         // $this->assign('typelist', $typeList);
         // $this->assign('shArray',$shArray);// 搜索参数输出
         $this->assign('pageno',$show);// 赋值分页输出    
         $this->assign('list', $list);
         $this->display();
    }


    /**
     * 添加用户界面
     */
    public function addAdmin() {
        // $info['username'] = I('username');
        // $info['password'] = I('password');
        // $info['status'] = I('status');
       $data['username']=I('uname');
       $data['password']=md5(I('pwd'));
       $data['status']=I('type');
       $data['add_time']=time();

              // var_dump($data);
     if ($_POST) {
        $re = M ('admin')->add ($data);

        if ($re) {
        $this->success("添加成功",U('Auth/userList'));
       }else{
         $this->error("失败");
       }       
        } else {
            $this->display();
        }

        // $this->display ();
    }

    /**
     * 添加管理员处理
     */
    public function addAdmindo() {
       $data['uname']=I('uname');
       $data['pwd']=md5(I('pwd'));
       $data['utype']=I('type');
       // $data['realname']=I('realname');
       // var_dump($data);
       // $mod=M('admin');
       // if($mod->add($data)){
       //     // $this->behaviorLog("成功添加用户");
       //      // $data['status']=100;
       //      // $data['info']="添加管理员成功！";
       //      // $this->ajaxReturn($data);
       //       $this->success("添加成功",U('Auth/roleList'));
       //  }else{
       //      // $this->behaviorLog("添加用户失败");
       //      // $data['status']=102;
       //      // $data['info']="添加管理员失败";
       //      // $this->ajaxReturn($data);
       //   $this->error("失败");
       //  }


    }



    /**
     * 用户管理
     */
    public function userList() {
        $from_time = I('from_time','');
        $to_time = I('to_time','');
        $uname =I("uname");
        if($uname){
            $where ="uname = {$uname}";
        }
          
        if($from_time){
            $from_time_s = strtotime($from_time);
            $where['login_time']= array('EGT',$from_time_s);
            $shArray['from_time'] = $from_time;
        }
        
        if($to_time){
            $to_time_s = strtotime($to_time)+86400;
            $where['login_time']= array('ELT',$to_time_s);
            $shArray['to_time'] = $to_time; 
        }       
        
        if($from_time && $to_time){
            $where['login_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
        }   
// ->where($where)
         $pageno = I('p',1);
         $pageCount = 10;
         $list = M('admin')->order('id desc')->page($pageno.','.$pageCount)->select();
         // var_dump($list);
        
         $count = M('admin')->where($where)->count();// 查询满足要求的总记录数
       
         $Page = new \Think\Page($count,$pageCount);// 实例化分页类 
         $show = $Page->show();// 分页显示输出
         $this->assign('pageno',$show);// 赋值分页输出    
         $this->assign('list', $list);
         $this->display();
    }

     /**
     * ajax判断 添加用户 数据库中有没有重名
     */
    public function Adminajax(){
        $mod = M('admin');
        $info = $mod->where("uname='{$_POST['name']}'")->find();      
        if($info){
           echo "1";
        }
    }
     /**
     * ajax判断 添加角色 数据库中有没有重名
     */
    public function Roleajax(){
        $mod = M('role');
        $info = $mod->where("name='{$_POST['name']}'")->find();      
        if($info){
           echo "1";
        }
    }


    
}